package de.dopichaj.labrador.genetic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public final class PatternFinderGUI extends JFrame {

    private static final long serialVersionUID = 1L;
    private final LineGraph lineGraph;
    private JLabel label;

    public PatternFinderGUI() {
        super("PatternFinder");
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 400);
        setLayout(new BorderLayout());
        lineGraph = new LineGraph();
        getContentPane().add(lineGraph);
        label = new JLabel();
        label.setText("Initializing, please wait ...");
        getContentPane().add(label, BorderLayout.SOUTH);
        setVisible(true);
    }

    private static final class LineGraph extends JPanel {
        
        private static final long serialVersionUID = 1L;
        private final List<Double> maxValues = new ArrayList<Double>();
        private final List<Double> avgValues = new ArrayList<Double>();
        private final List<Double> minValues = new ArrayList<Double>();

        public LineGraph() {
            super();
            setVisible(true);
            setOpaque(true);
            setBackground(Color.WHITE);
            setBorder(BorderFactory.createLoweredBevelBorder());
        }
        
        @Override
        public void paint(final Graphics g) {
            final Graphics2D graphics = (Graphics2D)g;
            
            graphics.setBackground(Color.WHITE);
            graphics.clearRect(0, 0, getWidth(), getHeight());
            drawLineGraph(graphics, maxValues, Color.GREEN);
            drawLineGraph(graphics, avgValues, Color.RED);
            drawLineGraph(graphics, minValues, Color.BLUE);
        }

        private void drawLineGraph(final Graphics2D graphics,
            final List<Double> list, final Color color) {
            
            final double xScale = (double)getWidth() / list.size();
            final double yScale = getHeight();
            graphics.setColor(color);
            for (int i = 1; i < list.size(); i++) {
                graphics.drawLine(
                    (int)(xScale * (i - 1)), (int)(yScale * (1 - list.get(i - 1))),
                    (int)(xScale * i), (int)(yScale * (1 - list.get(i))));
            }
        }

        public void addValues(final double max, final double avg, final double min) {
            maxValues.add(max);
            avgValues.add(avg);
            minValues.add(min);
            repaint();
        }
    }
    
    public void addValues(final double max, final double avg, final double min) {
        lineGraph.addValues(max, avg, min);
    }
    
    public void setMessage(final String text) {
        label.setText(text);
    }
    
    public static void main(String[] args) {
        new PatternFinderGUI();
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/